import * as vue from 'vue';

/**
 * View层基类
 *   核心封装vue生命周期、store、响应式操作
 */
abstract class BaseView {
    constructor() {
        // 注册生命周期
        vue.onBeforeMount(() => {
            this.$beforeMount();
        });
        vue.onMounted(() => {
            this.$mounted();
        });
        vue.onBeforeUpdate(() => {
            this.$beforeUpdate();
        });
        vue.onUpdated(() => {
            this.$updated();
        });
        vue.onBeforeUnmount(() => {
            this.$beforeUnmount();
        });
        vue.onUnmounted(() => {
            this.$unmounted();
        });
        vue.onActivated(() => {
            this.$activated();
        });
        vue.onDeactivated(() => {
            this.$deactivated();
        });
        vue.onErrorCaptured(() => {
            this.$errorCaptured();
        });
    }

    /**
     * 封装 vue getCurrentInstance
     *
     * @protected
     * @memberof BaseView
     */
    protected $getCurrentInstance = vue.getCurrentInstance;

    /**
     * 封装 vue watch
     *
     * @protected
     * @memberof BaseView
     */
    protected $watch = vue.watch;

    /**
     * 封装 vue computed
     *
     * @protected
     * @memberof BaseView
     */
    protected $computed = vue.computed;

    /**
     * 封装 vue ref
     *
     * @protected
     * @memberof BaseView
     */
    protected $ref = vue.ref;

    /**
     * 封装 vue toRaw
     *
     * @protected
     * @memberof BaseView
     */
    protected $toRaw = vue.toRaw;

    /**
     * 封装 vue reactive
     *
     * @protected
     * @memberof BaseView
     */
    protected $reactive = vue.reactive;

    /**
     * 封装 vue
     *
     * @protected
     * @memberof BaseView
     */
    protected $vue = vue;

    /**
     * 生命周期 onBeforeMount
     * 组件挂载到节点上之前执行的函数
     *
     * @protected
     * @memberof BaseView
     */
    protected $beforeMount() {
        console.log('base beforeMount');
    }

    /**
     * 生命周期 onMounted
     * 组件挂载完成后执行的函数
     *
     * @protected
     * @memberof BaseView
     */
    protected $mounted() {
        console.log('base mounted');
    }

    /**
     * 生命周期 onBeforeUpdate
     * 组件更新之前执行的函数
     *
     * @protected
     * @memberof BaseView
     */
    protected $beforeUpdate() {
        console.log('base beforeUpdate');
    }

    /**
     * 生命周期 onUpdated
     * 组件更新完成之后执行的函数
     *
     * @protected
     * @memberof BaseView
     */
    protected $updated() {
        console.log('base updated');
    }

    /**
     * 生命周期 onBeforeUnmount
     * 组件卸载之前执行的函数
     *
     * @protected
     * @memberof BaseView
     */
    protected $beforeUnmount() {
        console.log('base beforeUnmount');
    }

    /**
     * 生命周期 onUnmounted
     * 组件卸载完成后执行的函数
     *
     * @protected
     * @memberof BaseView
     */
    protected $unmounted() {
        console.log('base unmounted');
    }

    /**
     * 生命周期 onActivated
     * 被包含在 <keep-alive> 中的组件，会多出两个生命周期钩子函数，被激活时执行
     *
     * @protected
     * @memberof BaseView
     */
    protected $activated() {
        console.log('base activated');
    }

    /**
     * 生命周期 onDeactivated
     * 比如从 A 组件，切换到 B 组件，A 组件消失时执行
     *
     * @protected
     * @memberof BaseView
     */
    protected $deactivated() {
        console.log('base deactivated');
    }

    /**
     * 生命周期 onErrorCaptured
     * 当捕获一个来自子孙组件的异常时激活钩子函数
     *
     * @protected
     * @memberof BaseView
     */
    protected $errorCaptured() {
        console.log('base errorCaptured');
    }
}

export default BaseView;
